<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/17
 * Time: 11:38
 */

namespace YMApi\Model;


use Think\Model;

/**
 * Class SeminarModel
 * @package SDXApi\Model
 */
class SeminarModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'seminar_templates';

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	public function find_seminar_templates()
	{
		return $this->order('sort desc')->select();
	}

	/**
	 * Created by Jerry
	 * @param $name
	 * @return mixed
	 */
	public function find_seminar_template_by_name($name)
	{
		$where = array(
			'name'	=>	$name,
			'status'=>	1,
		);

		return $this->where($where)->order('sort desc')->find();
	}


	/**
	 * Created by Jerry
	 * @param $id
	 * @param int $is_show
	 * @return mixed
	 */
	public function find_seminar_template_by_id($id, $is_show = 0)
	{
		$time = time();

		$where = array(
			'id'		=>	$id,
			'status'	=>	1,
			'start_time'=>	array('LT', $time,),
			'end_time'	=>	array('GT', $time,),
		);

		if ($is_show) {
			unset($where['start_time'], $where['end_time']);
		}

		return $this->where($where)->find();
	}

    /**
     * 根据商品id获取参与的专题
     * Created by caicm
     * @param int $goods_id 商品id
     * @return mixed
     */
	public function find_seminar_template_by_goods_id($goods_id){

        $time = time();

        $where = array(
            't.status' => 1,
            'p.type' => 3,
            't.start_time'=>	array('LT', $time,),
            't.end_time'	=>	array('GT', $time,),
            'p.goods_id' => array('like','%"'.$goods_id.'"%')
        );

        return  M('seminarTemplates as t')
                ->field('t.*')
                ->join('left join sdx_seminar_template_pages as p on t.id=p.template_id')
                ->where($where)
                ->group('p.template_id')
                ->select();
    }
}